package com.sogeti.walkthrough.activities;

import com.sogeti.walkthrough.R;
import com.sogeti.walkthrough.service.IAndroidService;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
	private IAndroidService androidService;
    private Button serviceBtn;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        bindService(new Intent("com.sogeti.walkthrough.service.ANDROID_SERVICE"), serConn, Context.BIND_AUTO_CREATE);
        serviceBtn = (Button)findViewById(R.id.serviceBtn);
        serviceBtn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				callService();
			}
		});
    }
    private void callService(){
    	try{
    		EditText textValue = (EditText) findViewById(R.id.textArea);
    		String text = androidService.getAndroidText(textValue.getText().toString());
    		String other = "test";
    	}
    	catch(NullPointerException npe){
    		//TODO: Log Null pointer
    	}
    	catch(RemoteException rme){
    		//TODO: log errors
    	}
    }
    private ServiceConnection serConn = new ServiceConnection() {
		
		public void onServiceDisconnected(ComponentName name) {
			androidService = null;
			
		}
		
		public void onServiceConnected(ComponentName name, IBinder service) {
			androidService = IAndroidService.Stub.asInterface(service);
		}
	};
    
    public IAndroidService getAndroidService() {
		return androidService;
	}
	public void setAndroidService(IAndroidService androidService) {
		this.androidService = androidService;
	}
	
}